-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correctly infer x: SomeEnum; x.name
as union of literal names
#18797
base: master
Are you sure you want to change the base?
Conversation
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense to me
This comment has been minimized.
This comment has been minimized.
Hi! I am working on mypy in python/mypy#18797 And I've noticed a small thing in your code. Ideally, `SomeEnum.Item.name` should be inferenced as `Literal[NAME_ONE, NAME_TWO, ...]` But, since you later modify `status` (which is the name of a enum item), it should be explicitly annotated as `str`, not `Literal`.
I opened psycopg/psycopg#1023 to fix a "problem" is psycopg. |
This comment has been minimized.
This comment has been minimized.
Should this also handle unions of literals? I notice if you narrow with |
@TeamSpen210 please, share your examples, I would be happy to support them as well :) |
Here's an example of both: class SomeEnum(Enum):
A = 0
B = 1
C = 2
class AnotherEnum(Enum):
ONE = 1
TWO = 2
THREE = 3
def func(value: SomeEnum, combo: SomeEnum | Literal[AnotherEnum.ONE, AnotherEnum.THREE]):
reveal_type(value.name) # Literal["A", "B", "C"]
assert value is not SomeEnum.A
reveal_type(value.name) # Just str, but should be Literal["B", "C"]?
# also str, but could be Literal["A", "B", "C", "ONE", "THREE"]
reveal_type(combo.name) Second is a bit less likely, but seems reasonable to support? |
Ok, I see, supporting |
Currently enum names are strings, but this is being changed to become Literal. When this will happen, the type inferred will not be valid anymore, as we mutate it and manipulate it as string in the function. See python/mypy#18797
for more information, see https://pre-commit.ci
Done! |
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Closes #18786